#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

using namespace std;

int test_atol()
{
    string conv_src("\0");
    long int conv_des = atol(conv_src.c_str());
    printf("conv result is %ld", conv_des);
    return 0;
}

int test_string()
{
    string str("\0");
    cout << "string is: " << str << endl;
    return 0;
}

int memcpy_test()
{
    char des[20];
    const char *src = "hello word!";
    memset(des, 0, 6);
    memcpy(des, src, 0);
    cout << "memcpy 0 byte, des is: " << des << endl;
    memcpy(des, src, 5);
    cout << "memcpy 5 byte, des is: " << des << endl;
    return 0;
}

int ctime_test()
{
    char buf[50];

    time_t seconds=1647944196;

    char* str_time = ctime(&seconds);
    cout << "ctime result: " << str_time << endl;

    str_time = ctime_r(&seconds, buf);
    cout << "ctime_r result: " << str_time;
    cout << "ctime_r buf: " << buf << endl;

    return 0;
}

int reinit()
{
    int i, num=10;
    for(i=1; i<num+1; i++) {
        int b=i;
        cout << "b value is: " << b << endl;
    }
    return 0;
}

int file_operator()
{
    fstream testFile;
    testFile.open("test.txt", ios::in);
    if (testFile.is_open()) {
        // text file
        string line;
        getline(testFile, line);
        /*
        testFile.read(line.c_str(), 20);
        */
        cout << line << endl;
    }
    else {
        return -1;
    }
    testFile.close();

    return 0;
}

int scanf_parse(string url) {
    char host1[512];
    char host[512];
    char host0[512];
    int port;
    char path[512];
    int ret;

    //ret = sscanf(url.c_str(),"%*[^/]%[^?]?:%d/%s",host0,&port,path);
    //ret = sscanf(url.c_str(),"%*[^/]//%[^%]%[^:]:%d/%s",host0, host1, &port,path);
    ret = sscanf(url.c_str(),"%*[^/]//%[^%]%[^:]:%d/%s",host0, host1, &port,path);
    //sprintf(host0, "%s%s", host0, host1);
    strcat(host0, host1);
    cout << "[ret:" << ret << "]"
         << "[host:" << host << "]"
         << "[host0:" << host0 << "]"
         << "[host1:" << host1 << "]"
         << "[port:" << port << "]"
         << "[path:" << path << "]"
         << endl;
    return ret;
}

int test_scanf_parse() {
    //string url="ws://fe80:0:10:3:2102:6690:0:2%enp2s0?:4000/vvdm/socket/websocket";
    string url="ws://fe80:0:10:3:2102:6690:0:2%enp2s0:4000/vvdm/socket/websocket";
    return scanf_parse(url);
}

int main()
{
    //test_atol();
    //test_string();
    //memcpy_test();
    //reinit();
    test_scanf_parse();
    return 0;
}
